
package com.kugou.game.sdk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.core.PayManager;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.utils.Rx;

public class RechargeModeAdapter extends BaseAdapter {

    public static class ModeInfo {
        int modePicResId;

        int modeTextResId;

        public ModeInfo(int picResId, int textResId) {
            modePicResId = picResId;
            modeTextResId = textResId;
        }

        public int getModePicResId() {
            return modePicResId;
        }

        public void setModePicResId(int modePicResId) {
            this.modePicResId = modePicResId;
        }

        public int getModeTestResId() {
            return modeTextResId;
        }

        public void setModeTestResId(int modeTestResId) {
            this.modeTextResId = modeTestResId;
        }
    }

    private Context mContext;

    private ArrayList<ModeInfo> mModeInfos;

    public RechargeModeAdapter(ArrayList<ModeInfo> modeInfos, Context context) {
        mModeInfos = modeInfos;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mModeInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mModeInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(Rx.layout.kg_item_recharge_mode, null);
            holder.pic = (ImageView) convertView.findViewById(Rx.id.kg_recharge_mode_pic);
            holder.text = (TextView) convertView.findViewById(Rx.id.kg_recharge_mode_text);
            holder.otherInfoLayout = (RelativeLayout) convertView
                    .findViewById(Rx.id.kg_recharge_mode_other_layout);
            holder.otherInfo = (TextView) convertView
                    .findViewById(Rx.id.kg_recharge_mode_other_info);
            holder.redPoint = (ImageView) convertView
                    .findViewById(Rx.id.kg_recharge_mode_red_round);
            holder.divider1 = (View) convertView
                    .findViewById(Rx.id.kg_recharge_mode_divider_horizontal);
            holder.divider2 = (View) convertView
                    .findViewById(Rx.id.kg_recharge_mode_divider_vertical);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (SDKControler.getActivityOrientation() == ActivityOrientation.LANDSCAPE) {
            // 竖线，位于左边的显示
            if (position % 2 == 0) {
                holder.divider2.setVisibility(View.VISIBLE);
            } else {
                holder.divider2.setVisibility(View.GONE);
            }

            // 横线，最下一行不显示
            if (position / 2 == (getCount() - 1) / 2) {
                holder.divider1.setVisibility(View.GONE);
            } else {
                holder.divider1.setVisibility(View.VISIBLE);
            }
        }

        if (mModeInfos.get(position).modeTextResId == Rx.string.kg_recharge_mode_youbi
                && PayManager.hasYoubiRechargepreferential()) {
            holder.otherInfoLayout.setVisibility(View.VISIBLE);
            holder.otherInfo.setText(PayManager.getKubiRechargePreferentialTitle());

            if (PayManager.isShowRedPoint()) {
                holder.redPoint.setVisibility(View.VISIBLE);
            } else {
                holder.redPoint.setVisibility(View.INVISIBLE);
            }
        } else {
            holder.otherInfoLayout.setVisibility(View.GONE);
        }

        holder.pic.setImageResource(mModeInfos.get(position).modePicResId);
        holder.text.setText(mModeInfos.get(position).modeTextResId);

        return convertView;
    }

    class ViewHolder {
        ImageView pic;

        TextView text;

        RelativeLayout otherInfoLayout;

        TextView otherInfo;

        ImageView redPoint;

        View divider1;

        View divider2;
    }

}
